<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">

<!--        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materielTypeId" label="物料分类">-->
<!--          <a-input-number v-model="model.materielTypeId"/>-->
<!--        </a-form-model-item>-->

        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materielTypeId" label="物料分类">
          <a-select
            v-model="model.materielTypeId"
            placeholder="请选择"
            style="width: 175px"
            option-label-prop="label"
            @change="selectClick"
          >
            <a-select-option v-for="(item, index) in materielTypedataForm" :key="index" :value="item.id" :label="item.materielTypeName">
              <div slot="title" style="display: flex; flex-direction: row; justify-content: space-between">
                <div>{{ item.materielTypeName }}</div>
              </div>
            </a-select-option>
          </a-select>
        </a-form-model-item>



        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materielGoodsName" label="物料商品名称">
          <a-input placeholder="请输入物料商品名称" v-model="model.materielGoodsName" />
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="goodsImg" label="商品封面图">
          <j-image-upload class="avatar-uploader" text="封面图" v-model="model.goodsImg"></j-image-upload>
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="price" label="商品价格">
          <a-input-number v-model="model.price"/>
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="unitType" label="单位">
          <a-radio-group name="radioGroup" v-model="model.unitType" @change="timeTypeChange">
            <a-radio :value="0">个</a-radio>
            <a-radio :value="1">套</a-radio>
            <a-radio :value="2">瓶</a-radio>
            <a-radio :value="3">件</a-radio>
            <a-radio :value="4">条</a-radio>
          </a-radio-group>
        </a-form-model-item>
<!--        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="goodsDetails" label="商品详情">-->
<!--          <j-editor v-model="model.goodsDetails" />-->
<!--        </a-form-model-item>-->
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status" label="是否上架">
          <a-switch v-model="statusFlag" @change="statusClick()" />
        </a-form-model-item>

      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>
import { getAction, httpAction } from '@/api/manage'
  import moment from "moment"

  export default {
    name: "XhdjMaterielGoodsModal",
    data () {
      return {
        materielTypedataForm: [],
        title:"操作",
        visible: false,
        statusFlag: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules:{
        materielTypeId:[{ required: true, message: '请输入物料分类!' }],
        materielGoodsName:[{ required: true, message: '请输入物料商品名称!' }],
        price:[{ required: true, message: '请输入商品价格!' }],
        unitType:[{ required: true, message: '请选择单位类型' }],
        goodsImg:[{ required: true, message: '请上传商品图' }],

        },
        url: {
          add: "/material/xhdjMaterielGoods/add",
          edit: "/material/xhdjMaterielGoods/edit",
          allList: '/material/xhdjMaterielType/allList',
        },
      }
    },
    created () {
      this.xhdjMaterielTypeList()
    },
    methods: {
      timeTypeChange (item) {
        this.model.unitType = item.target.value
      },
      selectClick(item){
          this.model.materielTypeId = item
      },
      // 状态
      statusClick () {
        if (this.statusFlag) {
          this.model.status = 1
        } else {
          this.model.status = 0
        }
      },


      add () {
        //初始化默认值
        this.edit({});
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
        this.statusFlag = this.model.status === 1;

      },
      close () {
        this.$emit('close');
        this.visible = false;
        this.$refs.form.clearValidate();
      },
      xhdjMaterielTypeList () {
        getAction(this.url.allList).then(res => {
          if (res.code === 200) {
            this.materielTypedataForm = res.result
          }
        })
      },
      handleOk () {
        const that = this;
        // 触发表单验证
         this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }else{
             return false;
          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>

</style>